<template>
	<view>
		<view class="img-a">
			<view class="t-b">
				您好，
				<br />
				欢迎使用校园外卖-配送员端
			</view>
		</view>
		<view class="login-view" style="">
			<view class="t-login">
				<form class="cl">
					<view class="t-a">
						<text class="txt">手机号</text>
						<u-input type="number" height="120" name="phone" placeholder="请输入您的手机号" maxlength="11" v-model="phone" />
					</view>
					<view class="t-a">
						<text class="txt">密码</text>
						<u-input type="password" height="120" name="code" maxlength="18" placeholder="请输入您的密码" v-model="password" />
					</view>
					<button @tap="loginSubmit()">登 录</button>
					<!-- <view class="reg" @tap="reg()">注 册</view> -->
				</form>
			</view>
		</view>
	</view>
</template>
<script>
	import wxLogin from '@/common/mixins/wxLogin.js';
	import md5 from 'js-md5';
	import {
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				phone: '', //手机号码
				password: '', //密码
				code: '',
			};
		},
		// 混入微信登录
		// #ifdef H5
		mixins: [wxLogin],
		// #endif
		onLoad() {},
		methods: {
			...mapMutations(['login']),
			//判断是否是微信内置的浏览器
			isWXBrowser() {
				return (
					String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger"
				);
			},
			//当前登录按钮操作
			async loginSubmit() {
				// 验证放在接口前,实际用的时候放出来
				if (!this.phone) return this.$u.toast('请输入您的手机号');
				if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(this.phone)) return this.$u.toast('请输入正确手机号')
				if (!this.password) return this.$u.toast('请输入您的密码');
				let that = this;
				// 这些放在接口调用后, 也要存缓存,async在调用接口的时候可以放在 res前
				// 接口调用实例:
				this.$u.api.loginDeliveryInfo({
					phone: this.phone,
					password: md5(this.password),
					wxOptionId: uni.getStorageSync('riderUserInfo').wxOptionId
				}).then(async res => {
					if (res.code == 200) {
						if (res.data.phone == null) {
							uni.showModal({
								title: '温馨提示',
								content: '检测到该账号是第一次登陆，将绑定到该微信',
								showCancel: false,
								success: function(res) {
									uni.showToast({
										title: '登录成功',
										duration: 1200
									})
									setTimeout(()=>{
										that.login(res.data);
										uni.reLaunch({
											url: '/pages/rider/index'
										})
									})
								}
							});
						}else{
							uni.showToast({
								title: '登录成功',
								duration: 1200
							})
							this.login(res.data);
							await this.$ut.sleep(800);
							this.$ut.route('pages/rider/index', {}, 'reLaunch');
						}
					}
				})

				// uni.setStorageSync('userInfo',xxx)
				// uni.showToast({
				// 	title: '登录成功',
				// 	duration: 1200
				// })
				// await this.$ut.sleep(800);
				// this.$ut.route('pages/rider/index', {}, 'reLaunch');
			},
			//注册按钮点击
			reg() {
				this.$ut.route('pages/register/register');
			},
		}
	};
</script>
<style lang="scss" scoped>
	.txt {
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
	}

	.img-a {
		width: 100%;
		height: 450rpx;
		background-image: url('~@/static/images/head.png');
		background-size: 100%;
	}

	.reg {
		font-size: 28rpx;
		color: #fff;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 50rpx;
		font-weight: bold;
		background: #f5f6fa;
		color: #000000;
		text-align: center;
		margin-top: 30rpx;
	}

	.login-view {
		width: 100%;
		position: relative;
		margin-top: -120rpx;
		background-color: #ffffff;
		border-radius: 8% 8% 0% 0;
	}

	.t-login {
		width: 600rpx;
		margin: 0 auto;
		font-size: 28rpx;
		padding-top: 80rpx;
	}

	.t-login button {
		font-size: 28rpx;
		background: #2796f2;
		color: #fff;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 50rpx;
		font-weight: bold;
	}

	.t-login input {
		height: 90rpx;
		line-height: 90rpx;
		margin-bottom: 50rpx;
		border-bottom: 1px solid #e9e9e9;
		font-size: 28rpx;
	}

	.t-login .t-a {
		position: relative;
	}

	.t-b {
		text-align: left;
		font-size: 42rpx;
		color: #ffffff;
		padding: 130rpx 0 0 70rpx;
		font-weight: bold;
		line-height: 70rpx;
	}

	.t-login .t-c {
		position: absolute;
		right: 22rpx;
		top: 22rpx;
		background: #5677fc;
		color: #fff;
		font-size: 24rpx;
		border-radius: 50rpx;
		height: 50rpx;
		line-height: 50rpx;
		padding: 0 25rpx;
	}

	.t-login .t-d {
		text-align: center;
		color: #999;
		margin: 80rpx 0;
	}

	.t-login .t-e {
		text-align: center;
		width: 250rpx;
		margin: 80rpx auto 0;
	}

	.t-login .t-g {
		float: left;
		width: 50%;
	}

	.t-login .t-e image {
		width: 50rpx;
		height: 50rpx;
	}

	.t-login .t-f {
		text-align: center;
		margin: 150rpx 0 0 0;
		color: #666;
	}

	.t-login .t-f text {
		margin-left: 20rpx;
		color: #aaaaaa;
		font-size: 27rpx;
	}

	.t-login .uni-input-placeholder {
		color: #aeaeae;
	}

	.cl {
		zoom: 1;
	}

	.cl:after {
		clear: both;
		display: block;
		visibility: hidden;
		height: 0;
		content: '\20';
	}
</style>